#include "ProxyServerNetServerModule.h"
#include "ProxyServerNetServerPlugin.h"
#include "Protocol/Protocol.h"

bool FragrantGrass::ProxyServerNetServerModule::Init()
{
	m_pNetModule = m_pPluginManager->FindModule<INetModule>();
	m_pNetClientModule = m_pPluginManager->FindModule<INetClientModule>();
	m_pLogModule = m_pPluginManager->FindModule<ILogModule>();
	m_pProxyToWorldModule = m_pPluginManager->FindModule<IProxyServerToWorldModule>();
	m_pSecurityModule = m_pPluginManager->FindModule<ISecurityModule>();

    return true;
}

bool FragrantGrass::ProxyServerNetServerModule::AfterInit()
{
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_CONNECT_KEY, this, &FragrantGrass::ProxyServerNetServerModule::OnConnectKeyProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_WORLD_LIST, this, &FragrantGrass::ProxyServerNetServerModule::OnReqServerListProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_SELECT_SERVER, this, &FragrantGrass::ProxyServerNetServerModule::OnSelectServerProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_ROLE_LIST, this, &FragrantGrass::ProxyServerNetServerModule::OnReqRoleListProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_CREATE_ROLE, this, &FragrantGrass::ProxyServerNetServerModule::OnReqCreateRoleProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_DELETE_ROLE, this, &FragrantGrass::ProxyServerNetServerModule::OnReqDelRoleProcess);
	m_pNetModule->register_net_message_handler(Protocol::EGMI_REQ_ENTER_GAME, this, &FragrantGrass::ProxyServerNetServerModule::OnReqEnterGameServer);
	m_pNetModule->register_net_message_handler(this, &FragrantGrass::ProxyServerNetServerModule::OnOtherMessage);

	m_pNetModule->register_net_event_handler(this, &FragrantGrass::ProxyServerNetServerModule::OnSocketClientEvent);
	m_pNetModule->expand_buffer_size(1024*1024*2);

  //  FG_SHARE_PTR<NFIClass> xLogicClass = m_pClassModule->GetElement(NFrame::Server::ThisName());
  //  if (xLogicClass)
  //  {
		//const std::vector<std::string>& strIdList = xLogicClass->GetIDList();
		//for (int i = 0; i < strIdList.size(); ++i)
		//{
		//	const std::string& strId = strIdList[i];

  //          const int nServerType = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::Type());
  //          const int nServerID = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::ServerID());
  //          if (nServerType == FG_SERVER_TYPES::FG_ST_PROXY && m_pPluginManager->GetAppID() == nServerID)
  //          {
  //              const int nPort = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::Port());
  //              const int nMaxConnect = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::MaxOnline());
  //              const int nCpus = m_pElementModule->GetPropertyInt32(strId, NFrame::Server::CpuCount());
  //              //const std::string& strName = m_pElementModule->GetPropertyString(strId, NFrame::Server::ID());
  //              //const std::string& strIP = m_pElementModule->GetPropertyString(strId, NFrame::Server::IP());

  //              int nRet = m_pNetModule->Initialization(nMaxConnect, nPort, nCpus);
  //              if (nRet < 0)
  //              {
  //                  std::ostringstream strLog;
  //                  strLog << "Cannot init server net, Port = " << nPort;
  //                  m_pLogModule->LogNormal(ILogModule::LL_ERROR_NORMAL, NULL_OBJECT, strLog, __FUNCTION__, __LINE__);
  //                  FGASSERT(nRet, "Cannot init server net", __FILE__, __FUNCTION__);
  //                  exit(0);
  //              }
  //          }
  //      }
  //  }

    return true;
}

bool FragrantGrass::ProxyServerNetServerModule::Shut()
{
    return true;
}

bool FragrantGrass::ProxyServerNetServerModule::Execute()
{
	return true;
}

void FragrantGrass::ProxyServerNetServerModule::OnOtherMessage(const SOCK nSockIndex, const int nMsgID, const char * msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->getNet()->GetNetObject(nSockIndex);
	//if (!pNetObject || pNetObject->GetConnectKeyState() <= 0 || pNetObject->GetGameID() <= 0)
	//{
	//	//state error
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

	//Protocol::MsgBase xMsg;
	//if (!xMsg.ParseFromString(strMsgData))
	//{
	//	char szData[MAX_PATH] = { 0 };
	//	sprintf(szData, "Parse Message Failed from Packet to MsgBase, MessageID: %d\n", nMsgID);

	//	return;
	//}


	////real user id
	//*xMsg.mutable_player_id() = INetModule::FGToPB(pNetObject->GetUserID());


	//std::string strMsg;
	//if (!xMsg.SerializeToString(&strMsg))
	//{
	//	return;
	//}

	//if (xMsg.has_hash_ident())
	//{
	//	//special for distributed
	//	if (!pNetObject->GetHashIdentID().IsNull())
	//	{
	//		m_pNetClientModule->SendBySuitWithOutHead(FG_SERVER_TYPES::FG_ST_GAME, pNetObject->GetHashIdentID().ToString(), nMsgID, strMsg);
	//	}
	//	else
	//	{
	//		GUID xHashIdent = INetModule::PBToNF(xMsg.hash_ident());
	//		m_pNetClientModule->SendBySuitWithOutHead(FG_SERVER_TYPES::FG_ST_GAME, xHashIdent.ToString(), nMsgID, strMsg);
	//	}
	//}
	//else
	//{
	//	m_pNetClientModule->SendByServerIDWithOutHead(pNetObject->GetGameID(), nMsgID, strMsg);
	//}
}

void FragrantGrass::ProxyServerNetServerModule::OnConnectKeyProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    GUID nPlayerID;
    Protocol::ReqAccountLogin xMsg;
    if (!m_pNetModule->recieve_protobuf_message( nMsgID, msg, nLen, xMsg, nPlayerID))
    {
        return;
    }

	//bool bRet = m_pSecurityModule->VirifySecurityKey(xMsg.account(), xMsg.security_code());
 //   //bool bRet = m_pProxyToWorldModule->VerifyConnectData(xMsg.account(), xMsg.security_code());
 //   if (bRet)
 //   {
 //       NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
 //       if (pNetObject)
 //       {
 //           //this net-object verify successful and set state as true
 //           pNetObject->SetConnectKeyState(1);
	//		pNetObject->SetSecurityKey(xMsg.security_code());

 //           //this net-object bind a user's account
 //           pNetObject->SetAccount(xMsg.account());

 //           Protocol::AckEventResult xSendMsg;
 //           xSendMsg.set_event_code(Protocol::EGEC_VERIFY_KEY_SUCCESS);
 //           *xSendMsg.mutable_event_client() = INetModule::FGToPB(pNetObject->GetClientID());

	//		m_pNetModule->send_protobuf_message(Protocol::EGameMsgID::EGMI_ACK_CONNECT_KEY, xSendMsg, nSockIndex);
 //       }
 //   }
 //   else
 //   {
 //       //if verify failed then close this connect
	//	m_pNetModule->GetNet()->CloseNetObject(nSockIndex);
 //   }
}

void FragrantGrass::ProxyServerNetServerModule::OnSocketClientEvent(const SOCK nSockIndex, const FG_NET_EVENT eEvent, INet* pNet)
{
    if (eEvent & FG_NET_EVENT_EOF)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_EOF", "Connection closed", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else if (eEvent & FG_NET_EVENT_ERROR)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_ERROR", "Got an error on the connection", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else if (eEvent & FG_NET_EVENT_TIMEOUT)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_TIMEOUT", "read timeout", __FUNCTION__, __LINE__);
        OnClientDisconnect(nSockIndex);
    }
    else  if (eEvent & FG_NET_EVENT_CONNECTED)
    {
        m_pLogModule->LogNormal(ILogModule::LL_INFO_NORMAL, GUID(0, nSockIndex), "FG_NET_EVENT_CONNECTED", "connected success", __FUNCTION__, __LINE__);
        OnClientConnected(nSockIndex);
    }
}

void FragrantGrass::ProxyServerNetServerModule::OnClientDisconnect(const SOCK nAddress)
{
    //NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nAddress);
    //if (pNetObject)
    //{
    //    int nGameID = pNetObject->GetGameID();
    //    if (nGameID > 0)
    //    {
    //        //when a net-object bind a account then tell that game-server
    //        if (!pNetObject->GetUserID().IsNull())
    //        {
    //            Protocol::ReqLeaveGameServer xData;
				//xData.set_arg(nGameID);

    //            Protocol::MsgBase xMsg;

				////real user id
    //            *xMsg.mutable_player_id() = INetModule::FGToPB(pNetObject->GetUserID());

    //            if (!xData.SerializeToString(xMsg.mutable_msg_data()))
    //            {
    //                return;
    //            }

    //            std::string strMsg;
    //            if (!xMsg.SerializeToString(&strMsg))
    //            {
    //                return;
    //            }

				//m_pNetClientModule->SendByServerIDWithOutHead(nGameID, Protocol::EGameMsgID::EGMI_REQ_LEAVE_GAME, strMsg);
    //        }
    //    }

    //    mxClientIdent.RemoveElement(pNetObject->GetClientID());
    //}
}

void FragrantGrass::ProxyServerNetServerModule::OnSelectServerProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   GUID nPlayerID;
 //   Protocol::ReqSelectServer xMsg;
 //   if (!m_pNetModule->recieve_protobuf_message( nMsgID, strMsgData, xMsg, nPlayerID))
 //   {
 //       return;
 //   }

 //   FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(xMsg.world_id());
 //   if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
 //   {
	////Modify: not need check pNetObject again by wenmin
 //       //NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
 //       //if (pNetObject)
 //       //{
 //           //now this client bind a game server, all message will be sent to this game server whom bind with client
 //           pNetObject->SetGameID(xMsg.world_id());

 //           Protocol::AckEventResult xMsg;
 //           xMsg.set_event_code(Protocol::EGameEventCode::EGEC_SELECTSERVER_SUCCESS);
	//		m_pNetModule->send_protobuf_message(Protocol::EGameMsgID::EGMI_ACK_SELECT_SERVER, xMsg, nSockIndex);
 //           return;
 //       //}
 //   }

 //   //actually, if you want the game server working with a good performance then we need to find the game server with lowest workload
	//int nWorkload = 999999;
	//int nGameID = 0;
 //   NFMapEx<int, ConnectData>& xServerList = m_pNetClientModule->GetServerList();
 //   ConnectData* pGameData = xServerList.FirstNude();
 //   while (pGameData)
 //   {
 //       if (ConnectDataState::NORMAL == pGameData->eState
 //           && FG_SERVER_TYPES::FG_ST_GAME == pGameData->eServerType)
 //       {
	//		if (pGameData->nWorkLoad < nWorkload)
	//		{
	//			nWorkload = pGameData->nWorkLoad;
	//			nGameID = pGameData->nGameID;
	//		}
 //       }

 //       pGameData = xServerList.NextNude();
 //   }

	//if (nGameID > 0)
	//{
	//	pNetObject->SetGameID(nGameID);

	//	Protocol::AckEventResult xMsg;
	//	xMsg.set_event_code(Protocol::EGameEventCode::EGEC_SELECTSERVER_SUCCESS);
	//	m_pNetModule->send_protobuf_message(Protocol::EGameMsgID::EGMI_ACK_SELECT_SERVER, xMsg, nSockIndex);
	//	return;
	//}
	//

 //   Protocol::AckEventResult xSendMsg;
 //   xSendMsg.set_event_code(Protocol::EGameEventCode::EGEC_SELECTSERVER_FAIL);
	//m_pNetModule->send_protobuf_message(Protocol::EGameMsgID::EGMI_ACK_SELECT_SERVER, xMsg, nSockIndex);
}

void FragrantGrass::ProxyServerNetServerModule::OnReqServerListProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   if (pNetObject->GetConnectKeyState() > 0)
 //   {
	//	GUID nPlayerID;//no value
	//	Protocol::ReqServerList xMsg;
	//	if (!m_pNetModule->recieve_protobuf_message( nMsgID, strMsgData, xMsg, nPlayerID))
	//	{
	//		return;
	//	}

	//	if (xMsg.type() != Protocol::RSLT_GAMES_ERVER)
	//	{
	//		return;
	//	}

 //       //ack all gameserver data
 //       Protocol::AckServerList xData;
 //       xData.set_type(Protocol::RSLT_GAMES_ERVER);

 //       NFMapEx<int, ConnectData>& xServerList = m_pNetClientModule->GetServerList();
 //       ConnectData* pGameData = xServerList.FirstNude();
 //       while (pGameData)
 //       {
 //           if (ConnectDataState::NORMAL == pGameData->eState
	//			&& FG_SERVER_TYPES::FG_ST_GAME == pGameData->eServerType)
 //           {
 //               Protocol::ServerInfo* pServerInfo = xData.add_info();

 //               pServerInfo->set_name(pGameData->strName);
 //               pServerInfo->set_status(Protocol::EServerState::EST_NARMAL);
 //               pServerInfo->set_server_id(pGameData->nGameID);
 //               pServerInfo->set_wait_count(0);
 //           }

 //           pGameData = xServerList.NextNude();
 //       }

	//	m_pNetModule->send_protobuf_message(Protocol::EGameMsgID::EGMI_ACK_WORLD_LIST, xData, nSockIndex);
 //   }
}

int FragrantGrass::ProxyServerNetServerModule::Transpond(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    Protocol::MsgBase xMsg;
    if (!xMsg.ParseFromArray(msg, nLen))
    {
        char szData[MAX_PATH] = { 0 };
        sprintf(szData, "Parse Message Failed from Packet to MsgBase, MessageID: %d\n", nMsgID);

        return false;
    }

    //broadcast many palyers
   // for (int i = 0; i < xMsg.player_client_list_size(); ++i)
   // {
   //     FG_SHARE_PTR<SOCK> pFD = mxClientIdent.GetElement(INetModule::PBToFG(xMsg.player_client_list(i)));
   //     if (pFD)
   //     {
   //         if (xMsg.has_hash_ident())
   //         {
   //             NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(*pFD);
   //             if (pNetObject)
   //             {
   //                 pNetObject->SetHashIdentID(INetModule::PBToNF(xMsg.hash_ident()));
   //             }
   //         }

			//m_pNetModule->SendMsgWithOutHead(nMsgID, std::string(msg, nLen), *pFD);
   //     }
   // }

    //send message to one player
  //  if (xMsg.player_client_list_size() <= 0)
  //  {
		//GUID xClientIdent = INetModule::PBToNF(xMsg.player_id());
  //      FG_SHARE_PTR<SOCK> pFD = mxClientIdent.GetElement(xClientIdent);
  //      if (pFD)
  //      {
  //          if (xMsg.has_hash_ident())
  //          {
  //              NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(*pFD);
  //              if (pNetObject)
  //              {
  //                  pNetObject->SetHashIdentID(INetModule::PBToNF(xMsg.hash_ident()));
  //              }
  //          }

		//	m_pNetModule->SendMsgWithOutHead(nMsgID, std::string(msg, nLen), *pFD);
  //      }
		//else if(xClientIdent.IsNull())
		//{
		//	//send this msessage to all clientss
		//	m_pNetModule->GetNet()->SendMsgToAllClientWithOutHead(nMsgID, msg, nLen);
		//}
		////pFD is empty means end of connection, no need to send message to this client any more. And,
		////we should never send a message that specified to a player to all clients here.
		//else
		//{
		//}
    //}

    return true;
}

void FragrantGrass::ProxyServerNetServerModule::OnClientConnected(const SOCK nAddress)
{
	//bind client'id with socket id
    //GUID xClientIdent = m_pKernelModule->CreateGUID();
    //NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nAddress);
    //if (pNetObject)
    //{
    //    pNetObject->SetClientID(xClientIdent);
    //}

    //mxClientIdent.AddElement(xClientIdent, FG_SHARE_PTR<SOCK>(new SOCK(nAddress)));
}

void FragrantGrass::ProxyServerNetServerModule::OnReqRoleListProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   GUID nPlayerID;
 //   Protocol::ReqRoleList xData;
 //   if (!m_pNetModule->recieve_protobuf_message( nMsgID, msg, nLen, xData, nPlayerID))
 //   {
 //       return;
 //   }

 //   FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(xData.game_id());
 //   if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
 //   {
 //       if (pNetObject->GetConnectKeyState() > 0
 //           && pNetObject->GetGameID() == xData.game_id()
 //           && pNetObject->GetAccount() == xData.account())
 //       {
 //           Protocol::MsgBase xMsg;
 //           if (!xData.SerializeToString(xMsg.mutable_msg_data()))
 //           {
 //               return;
 //           }

	//		//clientid
 //           xMsg.mutable_player_id()->CopyFrom(INetModule::FGToPB(pNetObject->GetClientID()));

 //           std::string strMsg;
 //           if (!xMsg.SerializeToString(&strMsg))
 //           {
 //               return;
 //           }

	//		m_pNetClientModule->SendByServerIDWithOutHead(pNetObject->GetGameID(), Protocol::EGameMsgID::EGMI_REQ_ROLE_LIST, strMsg);
 //       }
 //   }
}

void FragrantGrass::ProxyServerNetServerModule::OnReqCreateRoleProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   GUID nPlayerID;//no value
 //   Protocol::ReqCreateRole xData;
 //   if (!m_pNetModule->recieve_protobuf_message( nMsgID, msg, nLen, xData, nPlayerID))
 //   {
 //       return;
 //   }

 //   FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(xData.game_id());
 //   if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
 //   {
 //       if (pNetObject->GetConnectKeyState() > 0
 //           && pNetObject->GetGameID() == xData.game_id()
 //           && pNetObject->GetAccount() == xData.account())
 //       {
 //           Protocol::MsgBase xMsg;
 //           if (!xData.SerializeToString(xMsg.mutable_msg_data()))
 //           {
 //               return;
 //           }
 //           
 //           //the clientid == playerid before the player entre the game-server
 //           xMsg.mutable_player_id()->CopyFrom(INetModule::FGToPB(pNetObject->GetClientID()));

 //           std::string strMsg;
 //           if (!xMsg.SerializeToString(&strMsg))
 //           {
 //               return;
 //           }

	//		m_pNetClientModule->SendByServerIDWithOutHead(pNetObject->GetGameID(), nMsgID, strMsg);
 //       }
 //   }
}

void FragrantGrass::ProxyServerNetServerModule::OnReqDelRoleProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   GUID nPlayerID;// no value
 //   Protocol::ReqDeleteRole xData;
 //   if (!m_pNetModule->recieve_protobuf_message( nMsgID, msg, nLen, xData, nPlayerID))
 //   {
 //       return;
 //   }

 //   FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(xData.game_id());
	//if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
 //   {
 //       if (pNetObject->GetConnectKeyState() > 0
 //           && pNetObject->GetGameID() == xData.game_id()
 //           && pNetObject->GetAccount() == xData.account())
 //       {
	//		Protocol::MsgBase xMsg;
	//		if (!xData.SerializeToString(xMsg.mutable_msg_data()))
	//		{
	//			return;
	//		}

	//		//clientid
	//		xMsg.mutable_player_id()->CopyFrom(INetModule::FGToPB(pNetObject->GetClientID()));

	//		std::string strMsg;
	//		if (!xMsg.SerializeToString(&strMsg))
	//		{
	//			return;
	//		}

	//		m_pNetClientModule->SendByServerIDWithOutHead(pNetObject->GetGameID(), nMsgID, strMsg);
 //       }
 //   }
}

void FragrantGrass::ProxyServerNetServerModule::OnReqEnterGameServer(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
	//NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	//if (!pNetObject)
	//{
	//	return;
	//}

	//std::string strMsgData = m_pSecurityModule->DecodeMsg(pNetObject->GetAccount(), pNetObject->GetSecurityKey(), nMsgID, msg, nLen);
	//if (strMsgData.empty())
	//{
	//	//decode failed
	//	return;
	//}

 //   GUID nPlayerID;//no value
 //   Protocol::ReqEnterGameServer xData;
 //   if (!m_pNetModule->recieve_protobuf_message( nMsgID, msg, nLen, xData, nPlayerID))
 //   {
 //       return;
 //   }

 //   FG_SHARE_PTR<ConnectData> pServerData = m_pNetClientModule->GetServerNetInfo(xData.game_id());
 //   if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
 //   {
 //       if (pNetObject->GetConnectKeyState() > 0
 //           && pNetObject->GetGameID() == xData.game_id()
 //           && pNetObject->GetAccount() == xData.account()
 //           && !xData.name().empty()
 //           && !xData.account().empty())
 //       {
 //           Protocol::MsgBase xMsg;
 //           if (!xData.SerializeToString(xMsg.mutable_msg_data()))
 //           {
 //               return;
 //           }

	//		//clientid
 //           xMsg.mutable_player_id()->CopyFrom(INetModule::FGToPB(pNetObject->GetClientID()));
 //           std::string strMsg;
 //           if (!xMsg.SerializeToString(&strMsg))
 //           {
 //               return;
 //           }

	//		m_pNetClientModule->SendByServerIDWithOutHead(pNetObject->GetGameID(), Protocol::EGameMsgID::EGMI_REQ_ENTER_GAME, strMsg);
 //       }
 //   }
}

int FragrantGrass::ProxyServerNetServerModule::EnterGameSuccessEvent(const GUID xClientID, const GUID xPlayerID)
{
    //FG_SHARE_PTR<SOCK> pFD = mxClientIdent.GetElement(xClientID);
    //if (pFD)
    //{
    //    NetObject* pNetObeject = m_pNetModule->GetNet()->GetNetObject(*pFD);
    //    if (pNetObeject)
    //    {
    //        pNetObeject->SetUserID(xPlayerID);
    //    }
    //}

    return 0;
}
